package com.lenovo.smarttraffic.util;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

/**
 * Created by Caleb.F
 * on 2019-12-26
 * at 14:07
 */
public class MyItemTouchHelperCallback extends ItemTouchHelper.Callback {
    public MyItemTouchHelperCallback() {
    }

    private  OnItemPositionListener mOnItemPositionListener;
    private static final String TAG = "MyItemTouchHelperCallba";
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlag;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        //网格布局的话swipeFlag应该为0,理解为不存在滑动，仅仅存在上下左右的拖拽
        if(layoutManager instanceof GridLayoutManager) {
            dragFlag=ItemTouchHelper.UP | ItemTouchHelper.DOWN |
                    ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            return makeMovementFlags(dragFlag,0);
        }
        else {
            return 0;
        }

    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPosition=viewHolder.getAdapterPosition();
        int targetPosition=target.getAdapterPosition();
        mOnItemPositionListener.onItemSwap(fromPosition,targetPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //此方法用于列表的recyclerView的滑动e
    }

    public interface OnItemPositionListener{
        void onItemSwap(int from, int target);
    }

    public void setOnItemPositionListener(OnItemPositionListener onItemPositionListener){
        mOnItemPositionListener=onItemPositionListener;
    }
}
